home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1883 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  84 lines

  1. Path: fnnews.fnal.gov!usenet
  2. From: Oleg Krivosheev <kriol>
  3. Newsgroups: comp.lang.c++
  4. Subject: vietual data in C++?
  5. Date: 13 Jan 1996 20:23:51 GMT
  6. Organization: FERMILAB, Batavia, IL
  7. Message-ID: <4d94cn$9vo@fnnews.fnal.gov>
  8. NNTP-Posting-Host: martian.fnal.gov
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.4 sun4c)
  13. X-URL: news:comp.lang.c++
  14.  
  15. Hi, Gentlemen
  16.  
  17. i want to implement something like virtual data (if such an idiom
  18. is already known, there must be it's own name). Let me show:
  19.  
  20. i define base class
  21.  
  22. template <class BaseData> class Base {
  23.  
  24.    ...
  25.  
  26.   // methods
  27.  
  28.    ...
  29.  
  30.   // data
  31.  
  32.   BaseData* pVirtualData;
  33. };
  34.  
  35. so now i can inherite concrete classes using different Data classes:
  36.  
  37. class D1: public Base<Data1> {
  38.   ...
  39. };
  40.  
  41. class D2: public Base<Data2> {
  42.   ...
  43. };
  44.  
  45. etc...
  46.  
  47. from the other hand BaseData, Data[i] must represent own hierarchy,
  48. but really contain only data and must have the same layout:
  49.  
  50. struct BaseData {
  51.   int i1;
  52.   int i2;
  53. }
  54.  
  55. struct Data1: public BaseData {
  56.   // ... only methods are added
  57. };
  58.  
  59. struct Data2: public BaseData {
  60.   // ... only methods are added
  61. };
  62.  
  63. etc, and i can use concrete classes D1, D2...
  64.  
  65. int main() {
  66.  D1 d1;
  67.  D2 d2;
  68.  
  69.  cerr << d1.pVirtualData->i1; // prints correspondent i1
  70.  cerr << d1.pVirtualData->i2; // prints correspondent i2
  71.  
  72. }
  73.  
  74. So, my questions are:
  75.  
  76. 1. Is such idiom already known? (under it's own name).
  77.  
  78. 2. What's wrong with above snippet?
  79.  
  80. Any comments are greatly appreciated
  81.  
  82.                                        Sincerely   Oleg
  83.  
  84.